package com.boarsoft.boar.batch.demo.flow.bigfile;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.boarsoft.boar.batch.demo.biz.Coredpfm01Biz;
import com.boarsoft.boar.batch.demo.entity.Coredpfm01;
import com.boarsoft.boar.batch.demo.util.ByteString;
import com.boarsoft.boar.batch.demo.util.ToBigDecimal;
import com.boarsoft.flow.core.bean.ProcessHandler;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

/**
 * @ClassName:  LoadDpfm01BatchSave   
 * @Description:被fork出的线程，每个线程里面批量save
 * @author: 
 * @date:   2019年2月15日 上午9:50:59   
 * @Copyright: 2019
 */
public class LoadDpfm01BatchSave extends SimpleBizImpl implements ProcessHandler {
	private static final Logger log = LoggerFactory.getLogger(LoadDpfm01BatchSave.class);

	@Autowired
	protected Coredpfm01Biz biz;

	private ExecutorService threadPool;

	@Override
	public Object process(String entry, Object data, Throwable throwable) throws Throwable {
		try {
			Map<String, Object> pm = (Map<String, Object>) data;
			final int MAX_COMMIT = (int) pm.get("commitCount");
			final String fileName = (String) pm.get("fileName");
			RandomAccessFile aFile = new RandomAccessFile(fileName, "r");
			FileChannel inChannel = aFile.getChannel();
			ByteBuffer buf = ByteBuffer.allocate(216);
			String[] stringField = new String[27];
			final List<Coredpfm01> list0 = new ArrayList<>();
			final List<Coredpfm01> list1 = new ArrayList<>();
			int i = 1;
			while (inChannel.read(buf) != -1) {
				buf.flip();
				String string = ByteString.getString(buf);

				stringField = string.split("\03");

				Coredpfm01 dpfm = new Coredpfm01();

				dpfm.setDM01ACCT(stringField[0]);
				dpfm.setDM01CLSFLG(stringField[1]);
				dpfm.setDM01DOCLST(stringField[2]);
				dpfm.setDM01PSWLST(stringField[3]);
				dpfm.setDM01ACTFLG(stringField[4]);
				dpfm.setDM01SECFLG(stringField[5]);
				dpfm.setDM01DOCFLG(stringField[6]);
				dpfm.setDM01VOUCLS(stringField[7]);
				dpfm.setDM01VOUNUM(stringField[8]);
				dpfm.setDM01WTTYPE(stringField[9]);
				dpfm.setDM01WTPRF1(stringField[10]);
				dpfm.setDM01INQPAS(stringField[11]);
				dpfm.setDM01WTPRF3(stringField[12]);
				dpfm.setDM01PRTNUM(ToBigDecimal.stringToBigDecimal(stringField[13]));
				dpfm.setDM01UPRTNU(ToBigDecimal.stringToBigDecimal(stringField[14]));
				dpfm.setDM01ACCNUM(ToBigDecimal.stringToBigDecimal(stringField[15]));
				dpfm.setDM01ACCSEQ(ToBigDecimal.stringToBigDecimal(stringField[16]));
				dpfm.setDM01ORGNO(stringField[17]);
				dpfm.setDM01CLIPRO(stringField[18]);
				dpfm.setDM01PBNUM(ToBigDecimal.stringToBigDecimal(stringField[19]));
				dpfm.setDM01LICNO(stringField[20]);
				dpfm.setDM01AUTNO(stringField[21]);
				dpfm.setDM01AVLDAT(stringField[22]);
				dpfm.setDM01FLAG(stringField[23]);
				dpfm.setDM01NODE(stringField[24]);
				dpfm.setLAST_ETL_ACG_DT(stringField[25]);
				dpfm.setDEL_F(ToBigDecimal.stringToLong(stringField[26]));

				if (getDataSource(Integer.valueOf(dpfm.getDM01CLSFLG())) == 0) {
					list0.add(dpfm);
				} else {
					list1.add(dpfm);
				}
				
				if (i % MAX_COMMIT == 0) {
//					System.out.println("每"+ MAX_COMMIT+ "行提交一次 ");
					biz.batchSave(0, list0);
					biz.batchSave(1, list1);
					list0.clear();
					list1.clear();
				}
				
				i++;

				stringField = null;
				string = null;
				buf.clear();
			}

//			System.out.println("最后提交一次 i = " + i + "最后一次行数：" + i % MAX_COMMIT);
			Future<Void> ft = threadPool.submit(new Callable(){
				@Override
				public Object call() throws Exception {
					biz.batchSave(0, list0);
					return null;
				}
				
			});
			
			biz.batchSave(1, list1);
			list0.clear();
			list1.clear();
			aFile.close();

			System.out.println("本线程装载完成: " + Thread.currentThread().getId() + "    "+ "处理的文件名称：" + fileName);
			System.out.println("本线程耗时:" + (System.currentTimeMillis() - LoadDpfm01Fork.loadDpfm01ForkStart) + "ms.");
		} catch (IOException e) {
			log.info(e.getMessage());
		}

		return data;
	}
	
	public int getDataSource(int tenantId) {
		return tenantId % 2;
	}
}
